﻿/**********************************************************************************
 * Project Name :   
 * Remarks      :  
 * Class Name   :  XmlExtensionMethodsTest 
 * Version      :  v1.0.0.0 
 * Author       :  Joey Liang 
 * Description  : 
 * Created Time :  2013/10/20 21:07:04
 * Update Man   :  Joey Liang
 * Update Time  :  2013/10/20 21:07:04
************************************************************************/

using IdleTest.MSTest;
using IdleTest.Xml;
using IdleTest.DemoBeTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Collections;

namespace IdleTest.DemoToUse
{
    [TestClass]
    public class XmlExtensionMethodsTest : BaseXmlTest
    {
        #region XML

        /// <summary>
        ///Find 的测试
        ///</summary>
        [TestMethod()]
        public void FindTest()
        {
            AssertCommon.IsEmpty<XmlNodeList, XmlNode>(new XmlNodeList[]{
                GetStubXmlNodeList(), GetStubXmlNodeList(null), GetStubXmlNodeList(null, null), 
                xmlBuilder.GetNodeList()
            }, true, p => p.Find(falseFunc));

            XmlNodeList nodeList = xmlBuilder.GetNodeList(DEFAULT_XPATH);
            XmlNode expectedNode = GetExpectedTureNode();
            XmlNode actualNode = nodeList.Find(trueFunc);
            AssertCommon.AreEqual(expectedNode, actualNode);
        }

        /// <summary>
        ///FindAll 的测试
        ///</summary>
        [TestMethod()]
        public void FindAllTest()
        {
            AssertCommon.IsEmpty<XmlNodeList, ICollection<XmlNode>>(new XmlNodeList[]{
                GetStubXmlNodeList(), GetStubXmlNodeList(null), GetStubXmlNodeList(null, null), 
                xmlBuilder.GetNodeList()
            }, true, p => p.FindAll(falseFunc));

            XmlNodeList nodeList = xmlBuilder.GetNodeList(DEFAULT_XPATH);
            XmlNodeList expectedNodes = GetExpectedTureNodes();
            ICollection<XmlNode> actualNodes = nodeList.FindAll(trueFunc);
            AssertCommon.AreEqual<IEnumerable>(expectedNodes, actualNodes);
        }

        /// <summary>
        ///GetAttribute 的测试
        ///</summary>
        [TestMethod()]
        public void GetAttributeTest()
        {
            XmlNode node = GetExpectedTureNode();
            GetAttributeTestHelper<string>(node, ATTR_VALUE, ATTR_NAME, p => node.GetAttribute(p));

            XmlNode attrValueNull = GetAttrValueNullNode();
            AssertCommon.IsEmpty<string>(attrValueNull.GetAttribute(ATTR_INT_NAME));
        }

        [TestMethod()]
        public void GetAttributeTest1()
        {
            XmlNode node = GetExpectedTureNode();
            GetAttributeTestHelper<string>(node, ATTR_VALUE, ATTR_NAME, p => node.GetAttribute<string>(p));

            node = xmlBuilder.GetNode(string.Format("{0}[@{1}='{2}']", DEFAULT_XPATH, ATTR_INT_NAME, ATTR_INT_VALUE));
            GetAttributeTestHelper<int>(node, ATTR_INT_VALUE, ATTR_INT_NAME, p => node.GetAttribute<int>(p));

            AssertCommon.AreEqual<string, int>(0, TestCommon.GetEmptyStrings(), true, p => node.GetAttribute<int>(p));

            XmlNode attrValueNull = GetAttrValueNullNode();
            int actual = attrValueNull.GetAttribute<int>(ATTR_INT_NAME);
            AssertCommon.AreEqual<int>(0, actual);
            AssertCommon.AreEqual(0, actual);
        }

        /// <summary>
        ///GetAttribute 的测试
        ///</summary>
        public void GetAttributeTestHelper<T>(XmlNode node, T expected, string attr, Func<string, T> testFunc)
        {
            AssertCommon.IsEmpty<string, string>(
                TestCommon.GetEmptyStrings(), true, p => node.GetAttribute(p));

            AssertCommon.AreEqual(expected, testFunc(attr));
        }
        #endregion
    }
}
